﻿using Power.Models;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    /// <summary>
    /// 成果表
    /// </summary>
    public class AchievementTableViewModel : ReportTableBase
    {
        public List<ResultTableData> Data { get; set; }
        private AchievementTable model;
        public AchievementTableViewModel(AchievementTable model)
        {
            this.model = model;
            Data = new List<ResultTableData>()
            {
                CreateEntity(20),
                CreateEntity(3),
                CreateEntity(4),
            };

            Init();
        }

        /// <summary>
        /// 动态创建测试数据
        /// </summary>
        /// <param name="subCount"></param>
        /// <returns></returns>
        public ResultTableData CreateEntity(int subCount)
        {
            var obj = Activator.CreateInstance(typeof(ResultTableData));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }

            var ret = obj as ResultTableData;
            ret.Achievements = new List<ResultTableBaseValue>();
            ret.RepairLevel = "II";
            ret.MaintainLevel = "IV";
            for (int i = 0; i < subCount; i++)
            {
                ret.Achievements.Add(CreateSubEntity());
            }
            return ret;
        }
        /// <summary>
        /// 动态创建子项测试数据
        /// </summary>
        /// <returns></returns>
        public ResultTableBaseValue CreateSubEntity()
        {
            var obj = Activator.CreateInstance(typeof(ResultTableBaseValue));

            var type = obj.GetType();
            var props = type.GetProperties();
            foreach (var item in props)
            {
                if (item.PropertyType == typeof(int))
                {
                    item.SetValue(obj, 1);
                }
                else if (item.PropertyType == typeof(string))
                {
                    item.SetValue(obj, "2");
                }
            }
            var ret = obj as ResultTableBaseValue;
            
            return ret;
        }
        public void Init()
        {
            var color1 = new SolidColorBrush(Color.FromRgb(0, 0, 255));
            var color2 = new SolidColorBrush(Color.FromRgb(255, 255, 0));
            var color3 = new SolidColorBrush(Color.FromRgb(255, 102, 0));
            var color4 = new SolidColorBrush(Color.FromRgb(255, 0, 0));

            var color5 = new SolidColorBrush(Color.FromRgb(200, 200, 200));

            Row = Data.SelectMany(p => p.Achievements).Count() + 3;
            Column = 52;

            var table = this.model.table;

            for (int i = 0; i < Row; i++)
            {
                table.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });

            }
            for (int i = 0; i < Column; i++)
            {
                table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
            }

            //画标题
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    DrawColumnSpanCell(table, i, j, "排水管道内窥检测与评估成果表", Column, 0, 0);


                    DrawRowSpanCell(table, i, j, "项目代码", 2, 1, 0);
                    DrawRowSpanCell(table, i, j, "子项", 2, 1, 1);
                    DrawRowSpanCell(table, i, j, "片区代码", 2, 1, 2);
                    DrawRowSpanCell(table, i, j, "区块代码", 2, 1, 3);
                    DrawRowSpanCell(table, i, j, "工点名称", 2, 1, 4);
                    DrawRowSpanCell(table, i, j, "起始井号", 2, 1, 5);
                    DrawRowSpanCell(table, i, j, "终止井号", 2, 1, 6);
                    DrawRowSpanCell(table, i, j, "地区参数", 2, 1, 7);
                    DrawRowSpanCell(table, i, j, "土质参数", 2, 1, 8);
                    DrawRowSpanCell(table, i, j, "敷设年代", 2, 1, 9);
                    DrawRowSpanCell(table, i, j, "管段类型", 2, 1, 10);
                    DrawRowSpanCell(table, i, j, "管段材质", 2, 1, 11);


                    DrawCell(table, i, j, "管段直径", 1, 12);
                    DrawCell(table, i, j, "管段长度", 1, 13);
                    DrawCell(table, i, j, "检测长度", 1, 14);
                    DrawCell(table, i, j, "起点埋深", 1, 15);
                    DrawCell(table, i, j, "终点埋深", 1, 16);

                    DrawColumnSpanCell(table, i, j, "m", 5, 2, 12);

                    DrawRowSpanCell(table, i, j, "缺陷起始位置", 2, 1, 17);
                    DrawRowSpanCell(table, i, j, "缺陷终止位置", 2, 1, 18);
                    DrawRowSpanCell(table, i, j, "缺陷长度", 2, 1, 19);
                    DrawRowSpanCell(table, i, j, "环向", 2, 1, 20);
                    DrawRowSpanCell(table, i, j, "缺陷类型", 2, 1, 21);
                    DrawRowSpanCell(table, i, j, "缺陷名称", 2, 1,22);
                    DrawRowSpanCell(table, i, j, "缺陷代码", 2, 1, 23);
                    DrawRowSpanCell(table, i, j, "缺陷等级", 2, 1, 24);
                    DrawRowSpanCell(table, i, j, "检测图片", 2, 1, 25);
                    DrawRowSpanCell(table, i, j, "检测描述", 2, 1, 26);
                    DrawRowSpanCell(table, i, j, "检测方法", 2, 1, 27);
                    DrawRowSpanCell(table, i, j, "检测方向", 2, 1, 28);
                    DrawRowSpanCell(table, i, j, "检测日期", 2, 1, 29);
                    DrawRowSpanCell(table, i, j, "检测批次", 2, 1, 30);
                    DrawRowSpanCell(table, i, j, "检察员", 2, 1, 31);
                    DrawRowSpanCell(table, i, j, "分包商", 2, 1, 32);
                    DrawRowSpanCell(table, i, j, "序号", 2, 1, 33);

                    DrawColumnSpanCell(table, i, j, "结构性",9 , 1, 34);
                    DrawColumnSpanCell(table, i, j, "功能性", 9, 1, 43);

                    
                    DrawCell(table, i, j, "S", 2, 34);
                    DrawCell(table, i, j, "Smax", 2, 35);
                    DrawCell(table, i, j, "缺陷参数", 2, 36);
                    DrawCell(table, i, j, "F等级", 2, 37);
                    DrawCell(table, i, j, "缺陷密度", 2, 38);
                    DrawCell(table, i, j, "SM类型", 2, 39);
                    DrawCell(table, i, j, "修复指数", 2, 40);
                    DrawCell(table, i, j, "修复等级", 2, 41);
                    DrawCell(table, i, j, "修复建议", 2, 42);

                    DrawCell(table, i, j, "Y", 2, 43);
                    DrawCell(table, i, j, "Ymax", 2, 44);
                    DrawCell(table, i, j, "缺陷参数", 2, 45);
                    DrawCell(table, i, j, "G等级", 2, 46);
                    DrawCell(table, i, j, "缺陷密度", 2, 47);
                    DrawCell(table, i, j, "YM类型", 2, 48);
                    DrawCell(table, i, j, "养护指数", 2, 49);
                    DrawCell(table, i, j, "养护等级", 2, 50);
                    DrawCell(table, i, j, "养护建议", 2, 51);

                    
                }
            }

            //画内容
            var firstRow = 3;
            var currentRow = 3;
            for (int i = 3; i < Data.Count+3; i++)
            {
                var item = Data[i-3];
                SolidColorBrush background = null;
                if (i % 2 == 1)
                    background = color5;
                for (int j = 0; j < item.Achievements.Count; j++)
                {
                    var row = item.Achievements[j];

                    var columnIndex = 0;

                    DrawCell(table, currentRow, columnIndex, row.Pid.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.SubProject, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.Area, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.Block, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.WorkField, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.StartWellName, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.EndWellName, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.AreaPara, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.SoilPara, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.LayYear, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.PipeType, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.PipeMate, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.PipeDia.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.PipeLen.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DetectLen.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.StartDep.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.EndDep.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.StartDefect.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.EndDefect.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectLen.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.Circumferential, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectType, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectName, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectCode, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectLev.ToString(), currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectImg, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectDes, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DetecMethod, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectDir, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectDate, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.DefectBat, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.Inspector, currentRow, columnIndex++,background);
                    DrawCell(table, currentRow, columnIndex, row.Subcontractors, currentRow, columnIndex++,background);

                    DrawRowSpanCell(table, currentRow, columnIndex, item.Order.ToString() , item.Achievements.Count, firstRow, columnIndex++,background);//33

                    DrawRowSpanCell(table, currentRow, columnIndex, item.Sparam, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.Smax, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.DefectParam, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.Flevel, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.StructDefectDens, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.SmType, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.RepairIndex, item.Achievements.Count, firstRow, columnIndex++,background);
                    if (item.RepairLevel == "I")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.RepairLevel, item.Achievements.Count, firstRow, columnIndex++,color1);
                    }
                    else if (item.RepairLevel == "II")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.RepairLevel, item.Achievements.Count, firstRow, columnIndex++, color2);

                    }
                    else if (item.RepairLevel == "III")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.RepairLevel, item.Achievements.Count, firstRow, columnIndex++, color3);

                    }
                    else if (item.RepairLevel == "IV")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.RepairLevel, item.Achievements.Count, firstRow, columnIndex++, color4);

                    }
                    else
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.RepairLevel, item.Achievements.Count, firstRow, columnIndex++,background);

                    }

                    DrawRowSpanCell(table, currentRow, columnIndex, item.RepairSuggest, item.Achievements.Count, firstRow, columnIndex++,background);

                    DrawRowSpanCell(table, currentRow, columnIndex, item.Yparam, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.Ymax, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.YdefectParam, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.Glevel, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.FuncDefectDens, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.YmType, item.Achievements.Count, firstRow, columnIndex++,background);
                    DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainIndex, item.Achievements.Count, firstRow, columnIndex++,background);

                    if (item.MaintainLevel == "I")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainLevel, item.Achievements.Count, firstRow, columnIndex++,color1);

                    }
                    else if (item.MaintainLevel == "II")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainLevel, item.Achievements.Count, firstRow, columnIndex++, color2);

                    }
                    else if (item.MaintainLevel == "III")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainLevel, item.Achievements.Count, firstRow, columnIndex++, color3);

                    }
                    else if (item.MaintainLevel == "IV")
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainLevel, item.Achievements.Count, firstRow, columnIndex++, color4);

                    }
                    else
                    {
                        DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainLevel, item.Achievements.Count, firstRow, columnIndex++,background);

                    }
                    DrawRowSpanCell(table, currentRow, columnIndex, item.MaintainSuggest, item.Achievements.Count, firstRow, columnIndex++,background);

                    currentRow++;
                }
                firstRow = currentRow;
            }

            //补边框
            DrawBorder(table);
        }
    }
}
